package com.cy.pj.sys.service.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Aspect
@Component
public class SysCacheAspect {
    private Map<String,Object> cache=new ConcurrentHashMap<String,Object>();

    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")
    public void doCache(){}
    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    public void doClearCache(){}
    @Around("doCache()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("Get data from cache");
        Object result=cache.get("deptKey");
        if(result!=null)return result;
        result=joinPoint.proceed();
        System.out.println("Put data to cache");
        cache.put("deptKey", result);
        return result;
    }
@AfterReturning("doClearCache()")
    public void doAfterReturning(){
        cache.clear();
    }

}
